﻿using Mengsk.ProxyDelivery.Domain;
using Mengsk.ProxyDelivery.Service;
using Mengsk.ProxyDelivery.Service.Spider;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Goods
{
    /// <summary>
    /// GoodsUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class GoodsUserControl : UserControl, IPlugin
    {
        private ShoesService shoesService = ServiceContainer.GetService<ShoesService>();
        private ShoesSkuService shoesSkuService = ServiceContainer.GetService<ShoesSkuService>();

        public GoodsUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "商品管理"; }
        }

        public Control UIControl
        {
            get { return this; }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.pbBar.Parameters.Clear();
                this.pbBar.Parameters.Add("id", this.tbId.Text.Trim());
                this.pbBar.Parameters.Add("vendorName", this.tbVendorName.Text.Trim());
                this.pbBar.Parameters.Add("number", this.tbNumber.Text.Trim());
                this.pbBar.Parameters.Add("type", this.cbbTypes.SelectedIndex);
                this.pbBar.StartPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            var window = new GoodsEditWindow();
            bool? ret = window.ShowDialog();
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var shoes = this.dgvShoes.SelectedItem as Mengsk.ProxyDelivery.Domain.Shoes;
                if (shoes == null)
                {
                    MessageBox.Show("请选择鞋子");
                    return;
                }
                var window = new GoodsEditWindow { Shoes = shoes };
                bool? ret = window.ShowDialog();
                if (ret != null && ret.Value)
                {
                    var item = this.dgvShoes.ItemsSource;
                    this.dgvShoes.ItemsSource = null;
                    this.dgvShoes.ItemsSource = item;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEnable_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var shoes = this.dgvShoes.SelectedItem as Mengsk.ProxyDelivery.Domain.Shoes;
                if (shoes == null)
                {
                    MessageBox.Show("请选择鞋子");
                    return;
                }
                if (MessageBox.Show("确定" + (shoes.Enabled ? "下架  " : "上架  ") + shoes.Number, "", MessageBoxButton.YesNo, MessageBoxImage.Question) != MessageBoxResult.Yes)
                {
                    return;
                }
                shoes.Enabled = !shoes.Enabled;
                this.shoesService.Update(shoes);
                var item = this.dgvShoes.ItemsSource;
                this.dgvShoes.ItemsSource = null;
                this.dgvShoes.ItemsSource = item;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pbBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            DataCollection<Shoes> item = null;
            if (string.IsNullOrWhiteSpace(e.GetParameter<string>("id")) == false)
            {
                var i = this.shoesService.GetById(int.Parse(e.GetParameter<string>("id")));
                item = new DataCollection<Shoes>();
                item.Total = i == null ? 0 : 1;
                item.Datas = new List<Shoes>();
                if (i != null)
                {
                    item.Datas.Add(i);
                }
            }
            else
            {
                item = this.shoesService.GetByAll(e.GetParameter<string>("vendorName"), e.GetParameter<string>("number"), e.GetParameter<int>("type"), 0, DateTime.MinValue, DateTime.MinValue, e.CurrentPage - 1, e.PageSize);
            }
            this.dgvShoes.ItemsSource = item.Datas;
            this.pbBar.Total = item.Total == 0 ? item.Datas.Count : item.Total;
            this.pbBar.CurrentCount = item.Datas.Count;
        }

        private void Open_Click(object sender, EventArgs e)
        {
            try
            {
                TextBlock tb = sender as TextBlock;
                if (tb == null)
                {
                    throw new Exception("Edit_Click错误，事件源不是TextBlock");
                }
                Mengsk.ProxyDelivery.Domain.Shoes vendor = tb.DataContext as Mengsk.ProxyDelivery.Domain.Shoes;
                Process.Start(vendor.Url.Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEditSku_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Shoes shoes = this.dgvShoes.SelectedItem as Shoes;
                if (shoes == null)
                {
                    MessageBox.Show("请选择鞋子");
                    return;
                }
                var window = new GoodsSkuEditWindow { Shoes = shoes };
                bool? ret = window.ShowDialog().Value;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                var shoes = this.dgvShoes.SelectedItem as Mengsk.ProxyDelivery.Domain.Shoes;
                if (shoes == null)
                {
                    MessageBox.Show("请选择鞋子");
                    return;
                }

                var vendor = ServiceContainer.GetService<VendorService>().GetById(shoes.VendorId);
                if (vendor == null)
                {
                    throw new Exception("厂家不存在");
                }
                SpiderBase sb = SpiderBase.CreateSpider(vendor.Spider);
                var shoess = sb.GetVendorShoes(vendor, shoes.Number);
                var s = shoess.FirstOrDefault(obj => obj.Type == shoes.Type && obj.Number == shoes.Number);
                if (s == null)
                {
                    throw new Exception("Go2上未找到相应的商品");
                }
                s.Id = shoes.Id;
                s.VendorId = shoes.VendorId;
                s.Comment = "";
                s.Weight = shoes.Weight;
                ServiceContainer.GetService<ShoesService>().UpdateWithSku(s);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
